package dao;

import entity.Orgnazition;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrgnazitionDao extends BaseDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    public List<Orgnazition> list(){
        //获取连接
        conn = super.getConnection();
        List<Orgnazition> list = new ArrayList<>();
        Orgnazition orgnazition = null;
        try{
            //SQL
            String sql = "select * from orgnazition order by createDate desc";
            //获取执行对象
            ps = conn.prepareStatement(sql);
            //执行SQL
            rs = ps.executeQuery();
            //处理结果集
            while (rs.next()){
                orgnazition = new Orgnazition();
                orgnazition.setId(rs.getInt("id"));
                orgnazition.setName(rs.getString("name"));
                orgnazition.setPrincipalName(rs.getString("principalName"));
                orgnazition.setStatus(rs.getInt("status"));
                orgnazition.setCreateDate(rs.getString("createDate"));
                list.add(orgnazition);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.closeAll(conn,ps,rs);
        }
        return list;
    }

    public int information(String name){
        //获取连接
        conn = super.getConnection();
        int count = 0;
        try{
            //SQL
            String sql = "select name from orgnazition where name=?";
            //获取执行对象
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            //执行SQL
            rs = ps.executeQuery();
            if(rs.next()){
                count = 1;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.closeAll(conn,ps,rs);
        }
        return count;
    }

    public int add(Orgnazition orgnazition){
        //获取连接
        conn = super.getConnection();
        int count = 0;
        try{
            //SQL
            String sql = "insert into orgnazition(`name`,principalName,status,createDate)"+
                         "values(?,?,0,?)";
            //获取执行对象
            ps = conn.prepareStatement(sql);
            ps.setString(1,orgnazition.getName());
            ps.setString(2,orgnazition.getPrincipalName());
            ps.setString(3,orgnazition.getCreateDate());
            //执行SQL
            count = ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.closeAll(conn,ps,rs);
        }
        return count;
    }

    public int update(int id,int status){
        //获取连接
        conn = super.getConnection();
        int count = 0;
        try{
            //SQL
            String sql = "update orgnazition set status=? where id=?";
            //获取执行对象
            ps = conn.prepareStatement(sql);
            ps.setInt(1,status);
            ps.setInt(2,id);
            //执行SQL
            count = ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.closeAll(conn,ps,rs);
        }
        return count;
    }
}
